package com.wbao.dianniu.ui;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import android.os.Bundle;
import android.text.Html;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout.LayoutParams;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.umeng.analytics.MobclickAgent;
import com.wbao.dianniu.customView.CustomDatePicker;
import com.wbao.dianniu.listener.IJobInviteListener;
import com.wbao.dianniu.logical.Const;
import com.wbao.dianniu.logical.GlobalContext;
import com.wbao.dianniu.manager.JobInviteManager;
import com.wbao.dianniu.utils.Notification;
import com.wbao.dianniu.R;
import com.wbao.dianniu.utils.Utils;

/**
 * 面试邀请
 *
 * @author yejianjun
 */
public class JobInviteActivity extends BaseActivity implements IJobInviteListener {

    private RelativeLayout rl_time;
    private EditText etPost;
    private TextView tvTime;
    private EditText etAddress;
    private Button sendBtn;
    private JobInviteManager manager;
    private int jobId;
    private CustomDatePicker customDatePicker1;
    private boolean isClick = false;
    private final int MAX_COUNT = 10;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentLayout(R.layout.activity_job_invite,
                LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
        setTitleName(getResources().getString(R.string.invited_interview));
        initView();
        initDatePicker();
        manager = new JobInviteManager(this);
        manager.addJobInviteListener(this);
        jobId = getIntent().getIntExtra(Const.INTENT_JOB_ID, 0);
    }

    private void initView() {
        rl_time = (RelativeLayout) findViewById(R.id.rl_job_time);
        etPost = (EditText) findViewById(R.id.et_job_post);
        tvTime = (TextView) findViewById(R.id.tv_job_time);
        etAddress = (EditText) findViewById(R.id.et_job_address);
        sendBtn = (Button) findViewById(R.id.job_invite_send);
        rl_time.setOnClickListener(this);
        sendBtn.setOnClickListener(this);
        Utils.ContentLengthLimit(this, etPost, MAX_COUNT);

    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.rl_job_time:
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.CHINA);
                String now = sdf.format(new Date());
                if (TextUtils.isEmpty(tvTime.getText().toString())) {
                    customDatePicker1.show(now);
                } else {
                    customDatePicker1.show(tvTime.getText().toString());
                }
                break;
            case R.id.job_invite_send:
                String post = etPost.getText().toString().trim();
                String time = tvTime.getText().toString().trim();
                String address = etAddress.getText().toString().trim();
                if (TextUtils.isEmpty(post)) {
                    Notification.toast(this, "填写岗位");
                    etPost.requestFocus();
                    return;
                }
                if (TextUtils.isEmpty(time)) {
                    Notification.toast(this, "选择面试时间");
                    tvTime.requestFocus();
                    return;
                }
                if (TextUtils.isEmpty(address)) {
                    Notification.toast(this, "填写面试地点");
                    etAddress.requestFocus();
                    return;
                }
                if (!isClick) {
                    isClick = true;
                    manager.jobInvite(GlobalContext.getAccountId(), jobId, post, time, address);
                }
                break;
            default:
                break;
        }

    }

    @Override
    public void onJobInviteFailure(int errorCode, String errorMsg) {
        // TODO Auto-generated method stub
        isClick = false;
    }

    @Override
    public void onJobInviteSuccess() {
        isClick = false;
        Notification.toast(this, "已成功邀请面试");
        finish();
    }

    private void initDatePicker() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.CHINA);
        String now = sdf.format(new Date());
        customDatePicker1 = new CustomDatePicker(this, new CustomDatePicker.ResultHandler() {
            @Override
            public void handle(String time) { // 回调接口，获得选中的时间
                tvTime.setText(time);
            }
        }, "2017-01-01 00:00", now); // 初始化日期格式请用：yyyy-MM-dd HH:mm，否则不能正常运行
        customDatePicker1.showSpecificTime(true); // 显示时和分
        customDatePicker1.setIsLoop(true); // 允许循环滚动
    }

    public void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    public void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (null != manager) {
            manager.removeJobInviteListener(this);
        }
    }
}
